package com.waimai.mobile.ui.activity;

import android.content.Intent;
import android.location.Address;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.waimai.mobile.R;
import com.waimai.mobile.api.WaiMaiServer;
import com.waimai.mobile.api.entity.ReceiveAddress;
import com.waimai.mobile.api.entity.User;
import com.waimai.mobile.api.model.ResponseModel;
import com.waimai.mobile.ui.WaiMaiApp;
import com.waimai.mobile.ui.adapter.AddressItemAdapter;

import org.androidannotations.annotations.AfterExtras;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.RestService;

import java.util.List;

/**
 * Created by lin on 2015/6/20.
 */
@EActivity(R.layout.activity_address_list)
public class AddressListActivity extends BaseActivity02{

    public static final int REQUEST_GET_ADDRESS = 21;
    public static final int RESULT_GET_SUCCESS = 22;
    public static final int RESULT_GET_NONE = 23;
    public static final int RESULT_GET_FAILED = 24;

    public static final String EXTRA_CHOOSE = "isChooseAddress";
    public static final String EXTRA_ADDRESS_ID = "addressId";
    public static final String EXTRA_DATA = "data";

    @App
    public WaiMaiApp app;

    @RestService
    public WaiMaiServer server;

    @Extra(EXTRA_CHOOSE)
    public boolean isChooseAddress = false;

    @Extra(EXTRA_ADDRESS_ID)
    public int addressId = -1;

    @Bean
    public AddressItemAdapter adapter;

    @ViewById
    public LinearLayout toAddresssContainerLL;

    @ViewById
    public ListView addressListLv;

    @ViewById
    public TextView mentionMsgTv;

    @AfterViews
    public void afterViews(){
        setActionBarTitle("我的收货地址");
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadAddresses();
    }

    public void loadAddresses(){
        loading();
        loadAddressList(app.getUser().getUserId());
    }

    @Click(R.id.toAddresssContainerLL)
    public void toAddresssContainerLLClick(){
        Intent intent = new Intent(this, AddressActivity_.class);
        startActivity(intent);
    }

    @Click(R.id.mentionMsgTv)
    public void mentionMsgTvClick(View v){
        loading();
        loadAddressList(app.getUser().getUserId());
    }

    @Background
    public void loadAddressList(Integer userId){
        try {
            ResponseModel<List<ReceiveAddress>> model = server.addresses(userId);
            if(model != null){
                switch (model.getStatus().getStatusCode()) {
                    case ResponseModel.STATUS_GET_DATA_SUCCESS:
                        loadedSuccess(model.getData());
                        break;
                    case ResponseModel.STATUS_GET_DATA_NONE:
                        loadedNoneData();
                        break;
                    case ResponseModel.STATUS_SERVER_ERROR:
                        serverError();
                        break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            netError();
        }
    }

    @UiThread
    public void loading(){
        addressListLv.setVisibility(View.GONE);
        mentionMsgTv.setText("正在加载地址...");
        mentionMsgTv.setVisibility(View.VISIBLE);
        mentionMsgTv.setClickable(false);
    }

    @UiThread
    public void loadedSuccess(List<ReceiveAddress> addresses){
        adapter.setAddresses(addresses);
        adapter.setChooseAddress(isChooseAddress);
        adapter.setChooseedAddressId(addressId);
        adapter.setOnChooseAddressListener(new AddressItemAdapter.OnChooseAddressListener() {
            @Override
            public void onChoosedAddress(ReceiveAddress address) {
                if(address != null) {
                    Intent intent = new Intent();
                    intent.putExtra(EXTRA_DATA, address);
                    setResult(RESULT_GET_SUCCESS, intent);
                    finish();
                }else{
                    setResult(RESULT_GET_FAILED);
                    finish();
                }
            }
        });

        addressListLv.setAdapter(adapter);
        addressListLv.setVisibility(View.VISIBLE);
        mentionMsgTv.setVisibility(View.GONE);
    }

    @UiThread
    public void loadedNoneData(){
        mentionMsgTv.setText("您还没有地址哦...");
    }

    @UiThread
    public void serverError(){
        mentionMsgTv.setText("服务器错误，点击重试");
        mentionMsgTv.setClickable(true);
    }

    @UiThread
    public void netError(){
        mentionMsgTv.setText("网络错误，点击重试");
        mentionMsgTv.setClickable(true);
    }

    @Override
    public void onBackPressed() {
        setResult(RESULT_GET_NONE);
        super.onBackPressed();
    }
}
